package file;

import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.io.output.FileWriterWithEncoding;

import java.io.*;
import java.util.Enumeration;
import java.util.concurrent.*;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipEntry;

/**
 * USER: lintc 【lintiancong@zhuojianchina.com】
 * DATE: 2017-04-28 14:41
 */
public class ZipUtil {

    public static void main(String[] args) {
        String filePath = "C:\\\\Users\\\\ucmed\\\\Desktop\\\\20170427access.log.tar.gz";
        readGzipFile(filePath);
    }

    public static void readGzipFile(String targzFile){
        FileWriterWithEncoding writer = null;
        try {
            writer = new FileWriterWithEncoding(new File("\\home\\nginxlogs\\access.log.20170427"),"utf-8",true);
        } catch (IOException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }
        FileInputStream fis = null;
        ArchiveInputStream in = null;
        BufferedInputStream bufferedInputStream = null;
        String f="";
        try {
            fis = new FileInputStream(targzFile);
            GZIPInputStream is = new GZIPInputStream(new BufferedInputStream(fis));
            in = new ArchiveStreamFactory().createArchiveInputStream("tar", is);
            bufferedInputStream = new BufferedInputStream(in);
            TarArchiveEntry entry = null;
            LinkedBlockingQueue queue = new LinkedBlockingQueue();
            final int maxsize = 10;
            ExecutorService executor = new ThreadPoolExecutor(maxsize, maxsize, 0l, TimeUnit.MILLISECONDS, queue);
            entry = (TarArchiveEntry) in.getNextEntry();
            long timestamp = 0;
            while (entry != null) {
                long size =0;
                String name = entry.getName();
                String[] names = name.split("/");
                String fileName = targzFile;
                for(int i = 0; i < names.length; i++) {
                    String str = names[i];
                    fileName = fileName + File.separator + str;
                    f=f+File.separator + str;

                    int time_stamp = 0;
                    if(!fileName.endsWith("/")){
                        // System.err.println("f>>>"+f+"<<>>"+fileName);
                        size = entry.getSize();
                        timestamp = (entry.getModTime()).getTime()/1000;
                    }
                    if (size > 0 ) {
                        StringBuffer line = new StringBuffer();
                        byte[] b = new byte[(int) entry.getSize()];
                        int len = 0;
                        while ((len = in.read(b)) != -1){
                            line.append(new String(b, 0, len, "utf-8"));
                        }
                        writer.write(line.toString());
                        // System.err.println("gzip>>>>"+line);
                        while (queue.size() >= maxsize) {
                            try {
                                Thread.sleep(10);
                            } catch (InterruptedException e) {
//                                Log.OutException(e);
                            }
                        }
//                        Callable worker = new AiRuiUser(userId, gender, age, province, city,timestamp);//此处为线程类
//                        executor.submit(worker);
                    }
                }
            }
        } catch (Exception e1) {
                // TODO Auto-generated catch block
//                updataFileStatus(f, targzFile, 0);
                e1.printStackTrace();
            }
        }
//        updataFileStatus(f, targzFile, 1);
//        f="";
//    }else{
//        f="";
//    }
//    entry = (TarArchiveEntry) in.getNextEntry();
//}
//in.close();
//        fis.close();
//        bufferedInputStream.close();
//        //fileIn.close();
//        executor.shutdown();
//        }
}
